package cn.autumnorange.app.money.provider.repository;

import cn.autumnorange.app.common.repository.jpa.BaseJpaRepository;
import cn.autumnorange.app.money.provider.entity.MoneyEntity;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;
import java.util.Optional;

@Repository
public interface MoneyJpaRepository extends BaseJpaRepository<MoneyEntity, Long> {

  MoneyEntity findByMoney(long money);

  @Override
  @Lock(LockModeType.PESSIMISTIC_WRITE)
  Optional<MoneyEntity> findById(Long id);

  /** 悲观锁实现②：手写SQL，结尾加上for update */
  @Query(value = "select t.* from money t where t.id = ?1 for update", nativeQuery = true)
  Optional<MoneyEntity> findTeacherForUpdate(Long id);
  //    @Override
  //    Optional<SysUserEntity> findById(Long aLong);
  //
  //    SysUserEntity findByUserName(String userName);
  //    List<SysUserEntity> findByUserNameAndAge(String userName, Integer age);
  //    List<SysUserEntity> findByUserNameLike(String userName);

}
